#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* detectCycle(struct ListNode* head) {
    if (head == NULL)
    {
        return NULL;
    }
    struct ListNode* cut = head;
    struct ListNode* mytail = head;
    int count = 0;
    while (mytail->next)
    {
        cut = head;
        if (mytail->next == mytail)
        {
            return mytail;
        }
        while (cut != mytail)
        {
            if (mytail->next == NULL)
            {
                return NULL;
            }
            if (mytail->next == cut)
            {
                return cut;
            }
            cut = cut->next;

        }
        mytail = mytail->next;
        count++;
    }
    return NULL;
}